package com.voltmao.controller.module.doc;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.voltmao.service.application.doc.DocTypeServiceImpl;
import com.voltmao.webpro.frame.analyze.DTOSmith;
import com.voltmao.webpro.frame.dto.ApiResult;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.frame.dto.GridViewResult;
import com.voltmao.webpro.frame.dto.status.Constants;
import com.voltmao.webpro.frame.exception.WebProViewException;
import com.voltmao.webpro.utils.http.HttpRequestUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * @author zya
 * 文档类型
 */
@Controller
@RequestMapping(value = "/module/doc/type")
public class DocTypeController {

    @Autowired
    DocTypeServiceImpl docTypeService;

    /**
     * 新增或更新文档类型
     * @param request
     * @return
     */
    @RequestMapping(value = "/insertOrUpdate.json")
    @ResponseBody
    public ApiResult insertOrUpdate(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                try{
                    return docTypeService.insertOrUpdate(params);
                }catch (Exception e){
                    apiResult.setStatus(Constants.PARAMETER_ERROR);
                    return e.getMessage();
                }
            }
        });
    }

    /**
     * 获取所有文档类型
     * @param request
     * @return
     */
    @RequestMapping(value = "/all.json")
    @ResponseBody
    public GridViewResult getDocType(HttpServletRequest request){
        return new DTOSmith(request).builderLoadGridViewResult(new DTOSmith.LoadGridViewBackCall() {
            @Override
            public String getDataUrl(HttpServletRequest request) {
                return "/search.json";
            }

            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);

                return docTypeService.getDocType(params);
            }

            @Override
            public Object defalutForm(HttpServletRequest request) throws WebProViewException {
                return null;
            }
        });
    }
}
